package com.mustafaiev.tair.cts.dao;

import java.util.List;

import org.apache.commons.lang.NotImplementedException;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.mustafaiev.tair.cts.exeptions.ContextCreationException;
import com.mustafaiev.tair.cts.exeptions.DataNotRemovedException;
import com.mustafaiev.tair.cts.exeptions.DataNotStoredException;
import com.mustafaiev.tair.cts.model.Basket;
import com.mustafaiev.tair.cts.model.BasketsBatch;
import com.mustafaiev.tair.cts.model.BasketsBatchFilter;

public class BasketsBatchDAO implements IBasketsBatchDAO {

	private static final String SELECT_ALL_FROM_BASKETS_BATCH = "from BasketsBatch";

	private static final Logger LOGGER = Logger
			.getLogger(BasketsBatchDAO.class);

	private final SessionFactory sessionFactory;

	public BasketsBatchDAO() throws ContextCreationException {
		try {
			this.sessionFactory = new Configuration().configure()
					.buildSessionFactory();
			LOGGER.info("Session factory created");
		} catch (final HibernateException e) {
			LOGGER.error(e);
			throw new ContextCreationException(
					"Hibernate Session was not created", e);
		}
	}

	@Override
	public void doSave(final BasketsBatch t) throws DataNotStoredException {
		final Session session = this.sessionFactory.openSession();
		final Transaction transaction = session.getTransaction();
		try {
			transaction.begin();
			session.saveOrUpdate(t);
			final List<Basket> baskets = t.getBaskets();
			for (final Basket basket : baskets) {
				basket.setBasketsBatch(t);
				session.saveOrUpdate(basket);
			}
			transaction.commit();
		} catch (final HibernateException e) {
			LOGGER.error(e);
			throw new DataNotStoredException(
					"Baskets batch was not stored for a some reason", e);
		}
	}

	@Override
	public void doDeleteById(final Long id) throws DataNotRemovedException {
		throw new NotImplementedException("doDeleteById() is not implemented");
	}

	@Override
	public void doDelete(final BasketsBatch t) throws DataNotRemovedException {
		final Session session = this.sessionFactory.openSession();
		final Transaction transaction = session.getTransaction();
		try {
			transaction.begin();
			session.delete(t);
			transaction.commit();
		} catch (final HibernateException e) {
			LOGGER.error(e);
			throw new DataNotRemovedException(
					"Baskets batch was not removed for a some reason", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BasketsBatch> retreiveBatches() {
		final Session session = this.sessionFactory.openSession();
		List<BasketsBatch> batches = null;
		try {
			batches = session.createQuery(SELECT_ALL_FROM_BASKETS_BATCH).list();
		} catch (final HibernateException e) {
			LOGGER.error(e);
		}
		return batches;
	}

	@Override
	public BasketsBatch retrieveBatch(final Long batchId) {
		final Session session = this.sessionFactory.openSession();
		BasketsBatch basketsBatch = null;
		try {
			basketsBatch = (BasketsBatch) session.get(BasketsBatch.class,
					batchId);
		} catch (final HibernateException e) {
			LOGGER.error(e);
		}
		return basketsBatch;
	}

	@Override
	public List<BasketsBatch> retrieveBatchesByFilter(
			final BasketsBatchFilter filter) {
		// TODO Implement me!!!
		return null;
	}

}
